package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.MerDish;
import com.example.wechattakeaway.entity.MerMenu;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.form.merdishform.InsertMerDishForm;
import com.example.wechattakeaway.form.merdishform.UpdateMerDishForm;
import com.example.wechattakeaway.mapper.MerDishMapper;
import com.example.wechattakeaway.mapper.MerMenuMapper;
import com.example.wechattakeaway.service.MerDishService;
import com.example.wechattakeaway.utils.FileUpload;
import com.example.wechattakeaway.utils.PageUtil;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Repository
public class MerDishServiceImpl implements MerDishService {

    @Autowired
    MerDishMapper merDishMapper;
    @Autowired
    MerMenuMapper merMenuMapper;
    /**
     * 插入菜品
     * @param insertMerDishForm
     * @return
     */
    @Override
    public ResultVO insertMerDish(InsertMerDishForm insertMerDishForm) {
        MerDish merDish=new MerDish();
        merDish.setDishName(insertMerDishForm.getDishName());
        merDish.setDishDescription(insertMerDishForm.getDishDescription());
        merDish.setDishPhoto(insertMerDishForm.getDishPhoto());
        merDish.setIsChoice(insertMerDishForm.getIsChoice());
        merDish.setDishPrice(insertMerDishForm.getDishPrice());
        merDish.setMenuId(insertMerDishForm.getMenuId());
        try {

            merDishMapper.insert(merDish);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    /**
     * 编辑菜品信息
     * @param updateMerDishForm
     * @return
     */
    @Override
    public ResultVO updateMerDish(UpdateMerDishForm updateMerDishForm) {
        MerDish merDish=new MerDish();
        merDish.setDishName(updateMerDishForm.getDishName());
        merDish.setDishDescription(updateMerDishForm.getDishDescription());
        merDish.setDishPhoto(updateMerDishForm.getDishPhoto());
        merDish.setIsChoice(updateMerDishForm.getIsChoice());
        merDish.setDishPrice(updateMerDishForm.getDishPrice());
        merDish.setDishId(updateMerDishForm.getDishId());
        try {

            merDishMapper.updateByPrimaryKeySelective(merDish);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    /**
     * 根据id查询菜品
     * @param dishId
     * @return
     */
    @Override
    public ResultVO selectDishById(Integer dishId) {
        MerDish dish=new MerDish();
        try {

           dish= merDishMapper.selectByPrimaryKey(dishId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success(dish);
    }

    /**
     * 根据菜单id查询菜品列表
     * @param menuId
     * @return
     */
    @Override
    public ResultVO selectDishByMenuId(Integer pageNum,Integer menuId) {
        PageHelper.startPage(pageNum,5);

        List<MerDish> list=new ArrayList<>();
        try {

           list= merDishMapper.selectByMenuId(menuId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(list);

        return ResultVOUtil.success(hashMap);
    }

    /**
     * 删除菜品
     * @param dishId
     * @return
     */
    @Override
    public ResultVO deleteMerDish(Integer dishId) {
        try {

            merDishMapper.deleteByPrimaryKey(dishId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }



    @Override
    public ResultVO selectDishByMerchantId(Integer merchantId) {
        List<MerMenu> merMenus=new ArrayList<>();

       // PageHelper.startPage(pageNum,5);

        List<MerDish> dishes=new ArrayList<>();
        try {

            merMenus=merMenuMapper.selectByMerchantId(merchantId);
            int[] ids=new int[merMenus.size()];
            for(int i=0;i<merMenus.size();i++){
                ids[i]=merMenus.get(i).getMenuId();
            }
            for(int j=0;j<ids.length;j++){
                List<MerDish> dishes1=new ArrayList<>();
                dishes1=merDishMapper.selectByMenuId(ids[j]);
                dishes.addAll(dishes1);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
//        hashMap = PageUtil.getMap(dishes);
        hashMap.put("list",dishes);

        return ResultVOUtil.success(hashMap);

    }

    @Override
    public ResultVO selectByMenuId(Integer menuId) {
       // PageHelper.startPage(pageNum,5);

        List<MerDish> list=new ArrayList<>();
        try {

            list= merDishMapper.selectByMenuId(menuId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
       // hashMap = PageUtil.getMap(list);
        hashMap.put("list",list);

        return ResultVOUtil.success(hashMap);
    }

    @Override
    public ResultVO UpdateDishPhotoById(MultipartFile file, Integer merchantId) {

        String fileName = FileUpload.UploadPicture(file);

        try{
            merDishMapper.UpdateDishPhotoById(fileName,merchantId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }
}
